package model;

import entity.Prestasi;
import servlet.ControllerLogin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import entity.Users;


//import controller.LogicAkun;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 
 */
public class Akun {

    Connection conn;
    Users user;

    public Akun(Connection conn) {
        this.conn = conn;
    }

    public Users getUser(String username) {
        Users user = new Users();
        String sql = "SELECT * FROM user WHERE EMAIL_USER = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setString(1, username);
            ResultSet rs = st.executeQuery();
            if (rs != null) {
                if (rs.next()) {
                    user.setIdUser(rs.getInt(1));
                    //user.setEmail(rs.getString(2));
                    user.setEmail("admintu@is.its.ac.id");
                    user.setAlamat(rs.getString(3));
                    //user.setPassword(rs.getString(4));
                    user.setPassword("admintu");
                    user.setTelepon(rs.getString(5));
                    
                    
                    user.setNama(rs.getString(6));
                    user.setEmail(rs.getString(7));
                    user.setAdminStat(rs.getInt(8));
                    user.setAdminStat(rs.getInt(9));
                    user.setAdminStat(rs.getInt(10));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    
    
    
    
    
    
    
    
    public void insertUser(String username, String password) throws Exception {
        String sql = "INSERT INTO users VALUES (NULL , NULL, NULL, NULL, NULL, ?, ?, 0)";
        try {
            PreparedStatement st = conn.prepareStatement(sql);

            /*st.setString(1, nama);
            st.setString(2, alamat);
            st.setString(3, telepon);
            st.setString(4, email);*/
            st.setString(1, username);
            st.setString(2, password);
            
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateUser(String nama, String alamat, String telepon, String email, String usernamebaru, String username) throws Exception {
        String sql = "UPDATE users SET NAMA=?, ALAMAT=?, TELEPON=?, EMAIL=?, USERNAME=? WHERE USERNAME=?";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, nama);
            st.setString(2, alamat);
            st.setString(3, telepon);
            st.setString(4, email);
            st.setString(5, usernamebaru);
            st.setString(6, username);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
    //change pass
    public void ubahPass(String password, String username) throws Exception {
        String sql = "UPDATE users SET PASSWORD=? WHERE USERNAME=?";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, password);
            st.setString(2, username);
            
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    

    public void deleteUser(int idUser) {

        String sql = "DELETE FROM users WHERE ID_USER = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, idUser);

            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
